package org.snowin.utils_base;


import org.apache.log4j.Logger;
import org.snowin.utils.GlobalInfo;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;



public class Environment {
    static Logger logger = Logger.getLogger(Environment.class);
    String rootPath = "";
    String sp = System.getProperty(GlobalInfo.FILE_SEPARATOR);

    private static Method addURL = initAddMethod();
    private static URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();


    private static Method initAddMethod() {
        Method add = null;


        try {
            add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
            add.setAccessible(true);
            return add;
        } catch (NoSuchMethodException e) {
            throw new RuntimeException();
        }

    }


    public void setRootPath() {
        File directory = new File("");
        String path = "";

        try {
            path = directory.getAbsolutePath();

        } catch (Exception e) {
            e.printStackTrace();
        }

        rootPath = path;
        GlobalInfo.rootPath = rootPath ;
    }
    public void loadAllJars() throws MalformedURLException {

        File libDir = new File( GlobalInfo.rootPath+ sp + "lib");
        ArrayList<String> includeJarFiles = new ArrayList<>();
        includeJarFiles.add("wtd.jar");
        includeJarFiles.add("dom4j-1.6.1.jar");
        includeJarFiles.add("jaxen-1.1.1.jar");
        includeJarFiles.add("log4j-1.2.17.jar");
        includeJarFiles.add("selenium-server-standalone-2.53.0.jar");
        includeJarFiles.add("activation-1.1.1.jar");
        includeJarFiles.add("javax.mail-1.5.6.jar");
        includeJarFiles.add("mysql-connector-java-5.0.5.jar");
        includeJarFiles.add("substance-7.3.jar");
        includeJarFiles.add("trident-1.3.jar");
        includeJarFiles.add("trident-7.3-swing.jar");

        File[] jarFiles = libDir.listFiles();
        for (File jarFile : jarFiles) {
            System.out.println(jarFile.getName());
            if(includeJarFiles.contains(jarFile.getName())){
                addURL(jarFile);
            }
        }
    }

    private void addURL(File file) throws MalformedURLException {

        try {
            addURL.invoke(classloader, new Object[]{file.toURI().toURL()});
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void build() {
        Class<?> mainClass = null;
        try {
            mainClass  = classloader.loadClass("org.snowin.common.Main");
            Method startMethod = mainClass.getMethod("start", URLClassLoader.class);

            startMethod.invoke(mainClass.newInstance(), classloader);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }
}
